<?php

class ProjectsController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout->setLayout('admin');
    }

    public function indexAction()
    {
        $obj = new Application_Model_Projects();
        $projects = $obj->getAllProjects();
        $this->view->projects = $projects;
    }

    public function updateAction()
    {
        $id = $this->_getParam('id', 0);
         //выводим форму
        $form = new Application_Form_Addproject();
        $this->view->form = $form;
        //если был передан корректный id
        if ($id > 0) {
            //делаем выборку данных указанного юзера
            //и заполняем ими форму
            $obj = new Application_Model_Projects();
            $project = $obj->getSelectProject($id);            
            $form->populate($project[0]);
            //передаем в вид логин юзера
            $this->view->name = $project[0]['name'];
            //если был POST запрос
            if ($this->getRequest()->isPost()) {
                $postdata = $this->getRequest()->getPost();
                //если форма заполнена верно
                if ($form->isValid($postdata)) {
                    
                    $file = $form->icon->getFileInfo();
                    $ext = pathinfo($file['icon']['name'], PATHINFO_EXTENSION);
                    //генерируем имя иконки
                    $small = rand().".".$ext;
                    $form->icon->addFilter('Rename', realpath(dirname('.')).
                    DIRECTORY_SEPARATOR.
                    'uploads/projects'.
                    DIRECTORY_SEPARATOR.
                    $small);
                    $form->icon->receive(); 

                    $file2 = $form->big_icon->getFileInfo();
                    $ext2 = pathinfo($file2['big_icon']['name'], PATHINFO_EXTENSION);
                    //генерируем имя иконки
                    $big = rand().".".$ext;
                    $form->big_icon->addFilter('Rename', realpath(dirname('.')).
                    DIRECTORY_SEPARATOR.
                    'uploads/projects'.
                    DIRECTORY_SEPARATOR.
                    $big);
                    $form->big_icon->receive();
                    
                    //получаем данные из POST и забиваем в массив
                    $data = array(
                        'name'        => $form->getValue('name'),
                        'type'        => $form->getValue('type'),
                        'date'        => $form->getValue('date'),
                        'client_name' => $form->getvalue('client_name'),
                        'url'         => $form->getValue('url'),
                        'about'       => $form->getValue('text'),
                        'id_category'  => $form->getValue('category'),
                        'little_img'  => $small,
                        'big_img'  => $big
                    );
                    //редактируем данные пользователя
                    $obj->updateSelectProject($id, $data);
                    $this->_helper->redirector('index','projects');
                }
            } 
        }    
    }

    public function addAction()
    {
        $obj = new Application_Model_Projects();
        $form = new Application_Form_Addproject();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $postData = $this->getRequest()->getPost();
            if ($form->isValid($postData)) {
                $file = $form->icon->getFileInfo();
                $ext = pathinfo($file['icon']['name'], PATHINFO_EXTENSION);
                //генерируем имя иконки
                $small = rand().".".$ext;
                $form->icon->addFilter('Rename', realpath(dirname('.')).
                DIRECTORY_SEPARATOR.
                'uploads/projects'.
                DIRECTORY_SEPARATOR.
                $small);
                $form->icon->receive(); 
                
                $file2 = $form->big_icon->getFileInfo();
                $ext2 = pathinfo($file2['big_icon']['name'], PATHINFO_EXTENSION);
                //генерируем имя иконки
                $big = rand().".".$ext;
                $form->big_icon->addFilter('Rename', realpath(dirname('.')).
                DIRECTORY_SEPARATOR.
                'uploads/projects'.
                DIRECTORY_SEPARATOR.
                $big);
                $form->big_icon->receive();
                                
                
                $data = array(
                    'name'        => $form->getValue('name'),
                    'type'        => $form->getValue('type'),
                    'date'        => $form->getValue('date'),
                    'client_name' => $form->getvalue('client_name'),
                    'url'         => $form->getValue('url'),
                    'about'       => $form->getValue('text'),
                    'id_category' => $form->getValue('category'),
                    'little_img'  => $small,
                    'big_img'  => $big
                );
                $id = $obj->addProject($data);
                //$additional = $form->additional->getFileInfo();                                
                $i = 0;               
                $adapter = $form->additional->getTransferAdapter();                                
                foreach ($adapter->getFileInfo() as $file) {
                    if ($i > 1) {
                    if ($file['name'] != null) {   
                      $ext = explode("[/\\.]", $file['name']);                  
                      $newName = rand().'.'.$ext[count($ext)-1];
                      $adapter->addFilter('Rename', realpath(dirname('.')).
                          DIRECTORY_SEPARATOR.
                          'uploads'.
                          DIRECTORY_SEPARATOR.
                          $newName);                      
                      $data2 = array(
                                'id_project' => $id,
                                'name'       => $newName
                            );                      
                        $obj->addImages($data2);
                        $adapter->receive($file['name']);
                      }                        
                    } 
                    $i++;
                }              
                $this->_helper->redirector('index', 'projects');
            }
        }
    }

    public function deleteAction()
    {
        $id = $this->_getParam('id');
        $obj = new Application_Model_Projects();
        $obj->deleteSelectProject($id);
        $this->_helper->redirector('index', 'projects');
    }


}







